package com.panda.web.controller.commons;

import com.alibaba.fastjson2.JSONObject;
import com.panda.common.core.controller.BaseController;
import com.panda.common.core.domain.R;
import com.panda.web.domain.vo.commons.ProvinceVO;
import com.panda.web.service.general.GeneralService;
import com.panda.web.utils.HttpClientUtil;
import lombok.RequiredArgsConstructor;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 公众号 公用接口Controller
 */
@CrossOrigin
@RestController
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RequestMapping("/general")
public class GeneralController extends BaseController {
    private final GeneralService generalService;

    /**
     * 返回特定格式的省市
     * @return
     */
    @GetMapping("/areaCity")
    public R getAreaCity(){
     List<ProvinceVO> provinceList = generalService.getAreaCity();
     return R.ok(provinceList,"ok");
    }
    @GetMapping("/share/config")
    public R getShareConfig(@RequestParam String url) {
        String appId = "wx83de228493c38c1c";
        String appSecret = "c9722f9217d91fda34f8667a56ae563b";
        String accessToken = getAccessToken(appId, appSecret);
        String jsapiTicket = getJsapiTicket(accessToken);

        long timestamp = System.currentTimeMillis() / 1000;
        String nonceStr = UUID.randomUUID().toString();
        String signature = generateSignature(jsapiTicket, nonceStr, timestamp, url);

        Map<String, String> config = new HashMap<>();
        config.put("appId", appId);
        config.put("timestamp", String.valueOf(timestamp));
        config.put("nonceStr", nonceStr);
        config.put("signature", signature);

        return R.ok(config,200,"ok");
    }

    @GetMapping("/ceshi/share/config")
    public R getShareConfigAAAA(@RequestParam String url) {
        String appId = "wx83de228493c38c1c";
        String appSecret = "c9722f9217d91fda34f8667a56ae563b";
        String accessToken = getAccessToken(appId, appSecret);
        String jsapiTicket = getJsapiTicket(accessToken);

        long timestamp = System.currentTimeMillis() / 1000;
        String nonceStr = UUID.randomUUID().toString();
        String signature = generateSignature(jsapiTicket, nonceStr, timestamp, url);

        Map<String, String> config = new HashMap<>();
        config.put("appId", appId);
        config.put("timestamp", String.valueOf(timestamp));
        config.put("nonceStr", nonceStr);
        config.put("signature", signature);

        return R.ok(config,200,"ok");
    }

    public String getAccessToken(String appId, String appSecret) {
        String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appId + "&secret=" + appSecret;
        String result = HttpClientUtil.get(url); // 使用 HTTP 工具类发送请求
        JSONObject jsonObject = JSONObject.parseObject(result);
        return jsonObject.getString("access_token");
    }
    public String getJsapiTicket(String accessToken) {
        String url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + accessToken + "&type=jsapi";
        String result = HttpClientUtil.get(url);
        JSONObject jsonObject = JSONObject.parseObject(result);
        return jsonObject.getString("ticket");
    }
    public String generateSignature(String jsapiTicket, String nonceStr, long timestamp, String url) {
        String string1 = "jsapi_ticket=" + jsapiTicket +
                "&noncestr=" + nonceStr +
                "&timestamp=" + timestamp +
                "&url=" + url;
        return DigestUtils.sha1Hex(string1); // 使用 Apache Commons Codec 的 SHA1 工具类
    }
}
